/**
 * 
 */
package dt.inlee.system.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.domain.Message;

/**
 * @功能说明：消息对象数据访问层
 * @作者： xgyin
 * @创建日期： 2010-11-14
 * @
 */
@Repository("messageDao")
public class MessageDao extends BaseDao {
	/**
	 * 根据人员ID,消息状态获取消息集合
	 * @param toId
	 * @param isRead
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<Message> getPersonMsg(String toId, String isRead) throws FrameworkException {
		String msgSql = "select message.id, message.fromId, message.toId, message.content, "
				+ "message.isRead, message.ownerId, message.edate, sysuser.realname as sendName, message.href  "
				+ "from message left join sysuser on message.fromId = sysuser.id " + "where message.toId='" + toId
				+ "' and message.ownerId = '" + toId + "' and message.isRead='" + isRead + "' order by edate desc";
		List<Object[]> result = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(msgSql)
				.list();
		List<Message> messages = new ArrayList<Message>();
		for (Object[] obj : result) {
			Message message = new Message();
			message.setId((String) obj[0]);
			message.setFromId((String) obj[1]);
			message.setToId((String) obj[2]);
			message.setContent((String) obj[3]);
			message.setIsRead((String) obj[4]);
			message.setOwnerId((String) obj[5]);
			message.setEdate((Date) obj[6]);
			message.setSendName((String) obj[7]);
			message.setHref((String) obj[8]);
			messages.add(message);

		}
		return messages;

	}

	/**
	 * 
	 * @param fromId
	 * @param toId
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<Message> getHistoryMsg(String fromId, String toId) throws FrameworkException {
		String msgSql = "select message.id, message.fromId, message.toId, message.content, "
				+ "message.isRead, message.ownerId, message.edate, sysuser.realname as sendName, message.href "
				+ "from message left join sysuser on message.fromId = sysuser.id " + "where ((message.toId = '" + toId
				+ "' and message.fromId = '" + fromId + "')" + " or (message.toId = '" + fromId
				+ "' and message.fromId = '" + toId + "'))" + " and message.ownerId = '" + fromId
				+ "' and message.isRead='yes' order by edate desc";
		List<Object[]> result = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(msgSql)
				.list();
		List<Message> messages = new ArrayList<Message>();
		for (Object[] obj : result) {
			Message message = new Message();
			message.setId((String) obj[0]);
			message.setFromId((String) obj[1]);
			message.setToId((String) obj[2]);
			message.setContent((String) obj[3]);
			message.setIsRead((String) obj[4]);
			message.setOwnerId((String) obj[5]);
			message.setEdate((Date) obj[6]);
			message.setSendName((String) obj[7]);
			message.setHref((String) obj[8]);
			messages.add(message);

		}
		return messages;
	}

	/**
	 * 清空历史信息
	 * @param fromId
	 * @param toId
	 * @throws FrameworkException
	 */
	public void clearHistoryMsgs(String fromId, String toId) throws FrameworkException {
		String msgSql = "DELETE FROM Message WHERE ((message.toId = '" + toId + "' AND message.fromId = '" + fromId
				+ "')" + " OR (message.toId = '" + fromId + "' AND message.fromId = '" + toId + "'))"
				+ " AND message.ownerId = '" + fromId + "' AND message.isRead='yes'";
		this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(msgSql).executeUpdate();

	}

	/**
	 * 根据 标示ID删除提醒信息
	 * @param taskID
	 * @throws FrameworkException
	 */
	public void deleteMessageByTaskID(String taskID) throws FrameworkException {
		String msgSql = "DELETE FROM Message WHERE taskID ='" + taskID + "'";
		this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(msgSql).executeUpdate();

	}

}
